﻿namespace In66.Net.Core.System.Json
{
    public static class SystemTextJson
    {
        /// <summary>
        /// 命名枚举
        /// </summary>
        public enum NameCase
        {
            Camel,
            Lower,
            Snake,
            Kebab
        }

        #region SystemTextJsonNamePolicy

        /// <summary>
        /// 返回对象全小写
        /// </summary>
        public class CamelCasePolicy : JsonNamingPolicy
        {
            /// <summary>
            /// 返回对象全小写
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public override string ConvertName(string name) =>
                name.ToCamelCase();
        }

        /// <summary>
        /// 返回对象全小写
        /// </summary>
        public class LowerCasePolicy : JsonNamingPolicy
        {
            /// <summary>
            /// 返回对象全小写
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public override string ConvertName(string name) =>
                name.ToLower();
        }

        /// <summary>
        /// 返回对象蛇形
        /// </summary>
        public class SnakeCasePolicy : JsonNamingPolicy
        {
            /// <summary>
            /// 返回对象蛇形
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public override string ConvertName(string name) => name.ToSnakeCase();
        }

        /// <summary>
        /// 返回对象中横线
        /// </summary>
        public class KebabCasePolicy : JsonNamingPolicy
        {
            /// <summary>
            /// 返回对象中横线
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public override string ConvertName(string name) => name.ToKebabCase();
        }

        #endregion

        public static JsonSerializerOptions GetDefaultOptions()
        {
            var options = new JsonSerializerOptions();
            options.Converters.Add(new DateTimeConverter());
            options.Converters.Add(new DateTimeNullableConverter());
            options.Encoder = GetDefaultEncoder();
            //该值指示是否允许、不允许或跳过注释。
            options.ReadCommentHandling = JsonCommentHandling.Skip;
            //dynamic与匿名类型序列化设置
            options.PropertyNameCaseInsensitive = true;
            //dynamic
            options.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
            //匿名类型
            options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;

            return options;
        }

        public static JsonNamingPolicy GetNamePolicy(NameCase nameCase)
        {
            switch (nameCase)
            {
                case NameCase.Camel:
                    return new CamelCasePolicy();
                case NameCase.Lower:
                    return new LowerCasePolicy();
                case NameCase.Snake:
                    return new SnakeCasePolicy();
                case NameCase.Kebab:
                    return new KebabCasePolicy();
                default:
                    return new SnakeCasePolicy();
            }

        }

        public static JavaScriptEncoder GetDefaultEncoder() => JavaScriptEncoder.Create(new TextEncoderSettings(UnicodeRanges.All));
    }
}
